require "basecamp"
require "rexml/document"

include REXML

Basecamp.establish_connection!(ARGV[0], ARGV[1], ARGV[2])

doc = Document.new("<lists />")
lists_element = doc.root

basecamp = Basecamp.new

basecamp.projects.each do |project|
	lists_element.attributes["name"] = project.name
	
	Basecamp::TodoList.all(project.id).each do |list|
		list_element = lists_element.add_element("list")
		list_element.attributes["name"] = list.name
		
		list.todo_items.each do |item|
			task_element = list_element.add_element("task")
			
			item.attributes.each do |key|
				if (key[0] == "creator_id" || key[0] == "responsible_party_id")
				
					if (item.responsible_party_type == "Person")
						p = basecamp.person(key[1])
						task_element.attributes[key[0]] =p.first_name + " " + p.last_name + " (" + key[1].to_s + ")"
					else
						# p = basecamp.company(key[1])
						# print("  " + key[0] + " => " + p.name + " (" + key[1].to_s + ")\n")
						# print("  " + key[0] + " => (" + key[1].to_s + ")\n")
						# print("  " + key[1])
					end
				elsif (key[0] == "id")
					task_element.attributes[key[0]] = "basecamp:" + key[1].to_s
				else
					task_element.attributes[key[0]] = key[1].to_s
				end
			end
		end
	end
end


doc.write($stdout, 1)